package com.chen.util;

import com.alibaba.fastjson.JSON;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * 公共servlet处理
 */
public class BaseServlet {

    public static String getPath
            (HttpServletRequest request) {
        //截取项目二级路径
        String uri = request.getRequestURI();
        String path = uri.substring(uri.lastIndexOf("/") + 1);
        return path;
    }

    /**
     * 输出json的方法
     *
     * @param response
     * @param obj      打包json对象
     * @throws IOException
     */
    private static void printJSON(HttpServletResponse response, Object obj) throws IOException {
        response.setContentType("application/json;charset=utf-8");
        String json = JSON.toJSONString(obj); //转json
        PrintWriter out = response.getWriter(); //获取输出
        out.print(json);
        out.close();
    }


}
